<template>
 <div>
   <!-- 面包屑导航器 -->
   <el-breadcrumb separator="/">
     <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
     <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
     <el-breadcrumb-item>商品列表</el-breadcrumb-item>
   </el-breadcrumb>
   <!-- 卡片视图区-->
   <el-card>
     <!-- 搜索与添加区 -->
     <el-row :gutter="20">
       <el-col :span="8">
         <el-input placeholder="请输入内容" @clear="getGoodList()" clearable v-model="queryInfo.query" class="input-with-select">
           <el-button  @click="getGoodList()"  slot="append" icon="el-icon-search"></el-button>
         </el-input>
       </el-col>
       <el-col :span="4">
         <el-button @click="goAddpage" type="primary">添加商品</el-button>
       </el-col>
     </el-row>
     <!--内容table区-->
     <el-table :data="goodList" style="width: 100%" border stripe>
       <el-table-column  type="index" >
       </el-table-column>
       <el-table-column prop="goods_name" label="商品名称" >
       </el-table-column>
       <el-table-column prop="goods_price"  width="100px" label="商品价格(元)" >
       </el-table-column>
       <el-table-column prop="goods_weight" width="100px" label="商品重量" >
       </el-table-column>
       <el-table-column prop="add_time"  width="130px"   label="创建时间" >
           <template slot-scope="scope">
             {{scope.row.add_time | dateFormat}}
           </template>
       </el-table-column>
       <el-table-column label="操作" width="250px">
         <template slot-scope="scope">
           <!--修改按钮-->
           <el-tooltip  effect="dark" content="修改商品信息" placement="top-start"  :enterable="false">
             <el-button @click="showEditDialog(scope.row.id)" type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
           </el-tooltip>
           <!--删除按钮-->
           <el-tooltip  effect="dark" content="删除商品" placement="top-start"  :enterable="false">
             <el-button type="danger" @click="removeGoodById(scope.row.goods_id)" icon="el-icon-delete"  size="mini" >删除</el-button>
           </el-tooltip>
         </template>
       </el-table-column>
     </el-table>
     <!-- 分页区域 -->
     <el-pagination
       @size-change="handleSizeChange"
       @current-change="handleCurrentChange"
       :current-page="queryInfo.pagenum"
       :page-sizes="[1, 2, 5, 10]"
       :page-size="queryInfo.pagesize"
       layout="total, sizes, prev, pager, next, jumper"
       :total="total" background>
     </el-pagination>
   </el-card>
 </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索内容 查询参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 添加商品对话框
      addDialogVisible: false,
      goodList: [],
      total: 0
    }
  },
  created () {
    this.getGoodList()
  },
  methods: {
    // 获取搜索的商品列表
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodList()
    },
    async removeGoodById (id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        return err
      })
      // 用户点删除 返回confirm  点击取消返回 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      // 删除操作
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败')
      this.$message.success('删除商品成功')
      this.getGoodList()
    },
    goAddpage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped>

</style>
